package com.linzhaowei.headhuntingservice;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.linzhaowei.headhuntingservice.adapter.JobInformationAdapter;
import com.linzhaowei.headhuntingservice.adapter.JobObjectiveAdapter;
import com.linzhaowei.headhuntingservice.bean.JobInformation;
import com.linzhaowei.headhuntingservice.bean.JobObjective;
import com.linzhaowei.headhuntingservice.utils.HttpUtils;
import com.linzhaowei.headhuntingservice.utils.Ip;

import java.util.ArrayList;
import java.util.List;

public class Delivered extends AppCompatActivity {

    RecyclerView recyclerView;
    List<JobInformation> jobInformationList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_delivered);
        initViews();

        getReceive();

    }

    private void initViews() {
        recyclerView = findViewById(R.id.delivered_list);
    }

    void getReceive() {
        new Thread(() -> {
            HttpUtils httpUtils = new HttpUtils();
            try {
                SharedPreferences sharedPreferences = getSharedPreferences("user", Context.MODE_PRIVATE);
                String result = httpUtils.get(Ip.ip + "/user/getDelivered?userId=" + sharedPreferences.getInt("id", -1));
                JSONArray jsonArray = JSON.parseArray(result);
                for (int i = 0; i < jsonArray.size(); i++) {
                    jobInformationList.add(jsonArray
                            .getJSONObject(i)
                            .toJavaObject(JobInformation.class));
                }

                runOnUiThread(this::show);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();
    }

    void show() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        JobInformationAdapter adapter = new JobInformationAdapter(jobInformationList);
        recyclerView.setAdapter(adapter);
    }
}
